﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.BanLe
{
    public partial class frmThanhToan : Form
    {
        public static bool kiemtra = false;
        bool checkClose = false;
        bool check = false;
        Entities.HDBanLe temp;

        public static Entities.HDBanLe hdbanle;
        public frmThanhToan()
        {
            InitializeComponent();
        }
        string hanhdong;

        public string Hanhdong
        {
            get { return hanhdong; }
            set { hanhdong = value; }
        }
        public frmThanhToan(string hanhdong, Entities.HDBanLe hdbanle)
        {
            InitializeComponent();
            this.hanhdong = Hanhdong;
            try
            {
                this.temp = hdbanle;
                tongphaitra = double.Parse(hdbanle.TongTienThanhToan) - double.Parse(hdbanle.GiaTriThe);
                lbTongTien.Text = lbKhachTra.Text = hdbanle.TongTienThanhToan;
                double abc = (double.Parse(hdbanle.TongTienThanhToan) - double.Parse(hdbanle.GiaTriThe));
                if (abc < 0)
                    txtKhachTra.Text = "0";
                else
                    txtKhachTra.Text = string.Format("{0:0.00}", abc);
                lbTheVip.Text = Common.Utilities.FormatMoney(hdbanle.GiaTriThe);
            }
            catch
            {
            }
        }

        void ThanhToan()
        {
            try
            {
                if (!Validat())
                {
                    return;
                }
                hdbanle = temp;
                checkClose = true;
                this.Close();

            }
            catch
            {
            }
        }

        void Thoat()
        {
            try
            {                
                this.Close();
            }
            catch
            {
            }
        }

        public double tongphaitra;

        public bool Validat()
        {
            try
            {
                if (double.Parse(txtKhachTra.Text) < tongphaitra)
                {
                    MessageBox.Show("Thanh toán không thể nhỏ hơn tổng phải trả");
                    return false;
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        private void frmThanhToan_Load(object sender, EventArgs e)
        {
            txtKhachTra.TextChanged += new EventHandler(txtKhachTra_TextChanged);
            this.FormClosing += new FormClosingEventHandler(frmThanhToan_FormClosing);
            btnThanhToan.Click += new EventHandler(btnThanhToan_Click);
            btnThoat.Click += new EventHandler(btnThoat_Click);
        }

        void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }

        void btnThanhToan_Click(object sender, EventArgs e)
        {
            ThanhToan();
        }

        private void txtKhachTra_TextChanged(object sender, EventArgs e)
        {
            try
            {
                double i = double.Parse(txtKhachTra.Text);
                if (i < 0)
                {
                    txtKhachTra.Text = "";
                    lbKhachTra.Text = "0";
                    lbDuTra.Text = "0";
                }
                lbKhachTra.Text = Common.Utilities.FormatMoney(txtKhachTra.Text);
                string dutra = (double.Parse(lbKhachTra.Text.ToString()) - tongphaitra).ToString();
                lbDuTra.Text = Common.Utilities.FormatMoney(dutra);

            }
            catch
            {
                txtKhachTra.Text = "";
                lbKhachTra.Text = "0";
                string dutra = (0 - tongphaitra).ToString();
                lbDuTra.Text = Common.Utilities.FormatMoney(dutra);
            }
        }

        void frmThanhToan_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!checkClose)
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    kiemtra = true;
                    e.Cancel = checkClose;
                }
                else
                    e.Cancel = !checkClose;
        }
    }
}
#endregion